
document.addEventListener('dblclick', enhanceGoogleDictionnary, false);
$(document).ready(renderPopup);

function getPopupStyle(){
	return "background: #FFFFFF;" +
			"border-bottom: 4px solid #93a5bc;" +
			"font-family: Arial;" +
			"font-style: normal;" +
			"position: fixed;" +
			"top: 0px;" +
			"left: 0px;" +
			"width: 100%;" +
			"text-align: center;" +
			"padding-top: 10px;" +
			"height: 30px;" +
			"font-size: 14px;" +
			"z-index: 999;" +
			"display: none;";
			
}

function getWordStyle(){
	return "font-weight: bold;" +
	"color: #93a5bc;";
}

function getWordRedStyle(){
	return "font-weight: bold;" +
	"color: #a01818;";
}

function getButtonStyle(){
	return "border:1px solid #5d7b97;" +
	"background-color:#7f9db9;" +
	"border-radius:6px;" +
	"font-weight: bold;" +
	"color: white;" +
	"padding-top: 2px;" +
	"padding-bottom: 2px;" +
	"padding-right: 10px;" +
	"padding-left: 10px;" +
	"margin-left: 10px;" +
	"cursor: pointer;";
}
	
function renderPopup(){
			chrome.extension.sendRequest({type: "getContent"}, function(response) {
				if (response && response.word){
					$('body')
					.append($('<div></div>')
						.addClass("popup")
						.attr("style", getPopupStyle())
						.append($('<span></span>')
							.attr("style", getWordStyle())
							.append(response.word + " : "))
						.append(response.definition)
						.append($('<span></span>')
							.attr("style", getButtonStyle())
							.click(function(){
								var mistake = response.fake;
								chrome.extension.sendRequest({type: "updateMistakes", word: response.word, mistake: mistake}, function(updateResponse) {
									if(mistake){
										manageMistake(response);			
									} else {
										$('div.popup').hide("normal");
									}
								});						
							})
							.text("Yes")
						)
						.append($('<span></span>')
							.attr("style", getButtonStyle())
							.click(function(){
								var mistake = !response.fake;
								chrome.extension.sendRequest({type: "updateMistakes", word: response.word, mistake: mistake}, function(updateResponse) {
									if(mistake){
										manageMistake(response);
									} else {
										$('div.popup').hide("normal");
									}
								});
							})
							.text("No")
						)
					);
				}
				$('div.popup').show("normal");
			});	
}
	

function manageMistake(response){
	$('div.popup').empty();
	$('div.popup')
		.append($('<span></span>')
			.attr("style", getWordRedStyle())
			.append(response.word + " : "))
			.append(response.realDefinition)
			.append($('<span></span>')
				.attr("style", getButtonStyle())
				.text("This was the right answer")
				.click(function(){
					$('div.popup').hide("normal");
				}));							
}


function enhanceGoogleDictionnary(){
	var timeSpan = 0;
	var timeInterval = 200;
	var privateEnhanceGD = function (){
		timeSpan += timeInterval;
		if($('div.gd-bubble') && $('div.gd-bubble div:nth-child(2) > b:nth-child(1)').text() != ""){
			var saveText = 'Save!';
			if($('span#learnId').text().indexOf(saveText) == -1){
				$("div > a:contains('More ')")
					.parent()
						.prepend($('<span></span>')
							.append(saveText)
							.attr("id", "learnId")
							.attr("title", "Save this word in Learn!")
							.attr("style", "margin-right: 5px; font-weight: bold; width: 20px; cursor: pointer; color: rgb(225, 198, 66);")
							.click(function(){
								var word = $('div.gd-bubble div:nth-child(2) > b:nth-child(1)').text();
								var definition = $('div.gd-bubble div:nth-child(2)').text();
								saveDefinition(word, definition);
							}));	
			}						
		} else if(timeSpan < 5000) {
			setTimeout(privateEnhanceGD, timeInterval);
		}
	};
	setTimeout(privateEnhanceGD, timeInterval);
}

function saveDefinition(word, definition){
	chrome.extension.sendRequest({type: "save", word: word, definition: definition}, function(response) {
		if(response && response.message){
			alert(response.message);
		}
	});
}